package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Client;
import com.bonuo.crm.domain.Newspaper;
import com.bonuo.crm.service.NewspaperService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

//周报controller
@RestController
@RequestMapping("/crm/newspaper")
@Api(tags = "周报")
public class NewspaperController extends BaseController {
    @Autowired
    private NewspaperService newspaperService;

    //查询周报列表
    @GetMapping("/list")
    @ApiOperation(value = "查询周报列表",httpMethod = "GET")
    public TableDataInfo list(Newspaper newspaper,Integer pageNum,Integer pageSize){
        if(pageNum != null && pageSize != null){
            startPage();
        }
        List<Newspaper> newspaperList = newspaperService.selectNewspaperList(newspaper);
        return getDataTable(newspaperList);
    }

    //添加周报
    @PostMapping("/add")
    @ApiOperation(value = "添加周报",httpMethod = "POST")
    public AjaxResult add(@RequestBody Newspaper newspaper){
        Integer add = newspaperService.insertNewspaper(newspaper);
        return toAjax(add);
    }

    //删除周报
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除周报",httpMethod = "DELETE")
    public AjaxResult delete(@PathVariable Integer id){
        Integer delete = newspaperService.deleteNewspaperById(id);
        return toAjax(delete);
    }

    //修改周报
    @PutMapping("/update")
    @ApiOperation(value = "修改周报",httpMethod = "PUT")
    public AjaxResult update(@RequestBody Newspaper newspaper){
        Integer update = newspaperService.updateNewspaper(newspaper);
        return toAjax(update);
    }

    //导出周报
    @PostMapping("/export")
    @ApiOperation(value = "导出周报",httpMethod = "POST")
    public void export(HttpServletResponse response, Newspaper newspaper) {
        List<Newspaper> list = newspaperService.selectNewspaperList(newspaper);
        ExcelUtil<Newspaper> util = new ExcelUtil<Newspaper>(Newspaper.class);
        util.exportExcel(response, list, "周报数据");
    }

    //查询本周所添加的各数据条数及列表
    @GetMapping("/weeklyData")
    @ApiOperation(value = "查询本周所添加的各数据条数及列表",httpMethod = "GET")
    public AjaxResult weeklyData(Integer userId){
        String map = newspaperService.weeklyData(userId);
        return success(map);
    }

    //根据ID查出对应内容
    @GetMapping("/selectNewspaperContentById")
    @ApiOperation(value = "根据ID查出对应内容",httpMethod = "GET")
    public AjaxResult selectNewspaperContentById(Integer id){
        String content = newspaperService.selectNewspaperContentById(id);
        if(!StringUtils.isNotBlank(content)){
            return success("内容为空");
        }
        // 2. 创建 ObjectMapper 实例
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            // 3. 将 JSON 字符串转回 Map<String, Object>
            // 注意：指定类型为 LinkedHashMap 可保留字段顺序（与存入时一致）
            Map<String, Object> map = objectMapper.readValue(content, LinkedHashMap.class);
            return success(map);

        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException("JSON 转换为 Map 失败", e);
        }
    }
}
